// #include "dl485_fl.h"
// #include <stdlib.h>

// static int lite_voice_player_debug(char *type,char *fun,char *param);
// xp_bool dl485_voice_init()
// {
//     xp_cli_debug_add(lite_voice_player_debug);
//     return xp_true;
// }

// /**
//  * @brief lite dl485语音模块设置语音提示
//  * 
//  * @param dev modbus设备
//  * @param item 播放语音编号
//  * @return xp_bool 成功返回xp_true,否则返回xp_false
//  */
// xp_bool dl485_voice_play(struct xp_modbus_device *dev, int item)
// {
//     if (dev == NULL || dev->inited != 1) return xp_false;
//     unsigned short addr = 0x0004;
//     unsigned short data = item;
//     int ret = dev->bus->swrite(dev, addr, data);

//     return (ret != 0) ? xp_false : xp_true;
// }

// static int lite_voice_player_debug(char *type,char *fun,char *param)
// {
//     if (strcmp(type, "voice_play") != 0) return 0;
//     struct xp_modbus_device *dev = get_modbus_device_by_name((unsigned char *)"voice_player");
//     dl485_voice_play(dev, atoi(fun));
//     return 1;
// }


#include "dl485_fl.h"
#include "bsp.h"

Type_dl485_Def dl485[] = {
    [0] = {
        .com.slave_id = 1,
        .com.port = 2,
        .com.speed = 9600,

        .isInitOk = false,
    },
    [1] = {
        .com.slave_id = 2,
        .com.port = 2,
        .com.speed = 9600,

        .isInitOk = false,
    },
};

//保持写寄存器
int xp_dl485_write_reginster(Type_dl485_Def * const handler, uint16_t addr, uint16_t *data, uint8_t len)
{
    int sta = 0;
    MB_data modbus;
    uint8_t try_times = 3;

    modbus.slave_id = handler->com.slave_id;
    modbus.mb_fun = mb_hold_w;
    modbus.start_addr = addr;
    modbus.wBuf = data;
    modbus.rwLen = len;
    modbus.resBuf = handler->com.rec_data;
    modbus.resLen = &handler->com.rec_len;
    while(try_times--){
        if( 0 == xp_modbus_ask(handler->com.port, &modbus) ) return 0;
    }

    return -1;
}
//保持读寄存器
int xp_dl485_read_reginster(Type_dl485_Def * const handler, uint16_t addr, uint16_t read_len, uint8_t *rec_data, uint8_t *rec_len)
{
    int sta = 0;
    MB_data modbus;
    uint8_t try_times = 3;

    modbus.slave_id = handler->com.slave_id;
    modbus.mb_fun = mb_hold_r;
    modbus.start_addr = addr;
    modbus.rwLen = read_len;
    modbus.resBuf = rec_data;
    modbus.resLen = rec_len;
    while(try_times--){
        if( 0 == xp_modbus_ask(handler->com.port, &modbus) ) return 0;
    }
	println("dl485 read fail !!!");
    return -1;
}

int dl485_voice_init(void)
{
	int ret = 0;
	
    for (uint8_t i = 0; i < 2; i++)
    {
        //初始化端口modbus
        ret = xp_modbus_init(dl485[i].com.port, dl485[i].com.speed, MB_PAR_NONE, 100);
        
        if(0 != ret){
            println("dl485 <id:%d> modbus init fail ! ret = %d", dl485[i].com.slave_id, ret);
            return ret;
        }else if ( 0 == (ret = xp_dl485_read_reginster(&dl485[i], ADDR_TEST, 1, dl485[i].com.rec_data, &dl485[i].com.rec_len)) ){
            dl485[i].isInitOk = true;
            println("dl485 <id:%d> init success", dl485[i].com.slave_id);
        }else{
            println("dl485 <id:%d> modbus init success, but read data failed ! ret = %d", dl485[i].com.slave_id, ret);
        }
    }
    return 0;
}

/**
 * @brief lite dl485语音模块设置语音提示
 * 
 * @param dev modbus设备
 * @param item 播放语音编号
 * @return 
 */
int dl485_voice_play(uint8_t id, int item)
{
    uint16_t data = item;
    Type_dl485_Def *handle = NULL;
    switch (id)
    {
    case 0: handle = &dl485[0]; break;
    case 1: handle = &dl485[1]; break;
    default:
        break;
    }

    return xp_dl485_write_reginster(handle, ADDR_VOICE_PLAY, &data, 1);
}

int dl485_voice_set_volume(uint8_t id, uint8_t volume)
{
    uint16_t data = volume;Type_dl485_Def *handle = NULL;
    switch (id)
    {
    case 0: handle = &dl485[0]; break;
    case 1: handle = &dl485[1]; break;
    default:
        break;
    }
    if(data > 30) data = 30;
    return xp_dl485_write_reginster(handle, ADDR_PLAY_VOLUME, &data, 1);
}
